From b878ff1db20ff9e287a9ac60cd772db22c364424 Mon Sep 17 00:00:00 2001
From: Vladislav Tarakanov <vladislav.tarakanov@bk.ru>
Date: Thu, 21 Mar 2024 12:49:56 +0400
Subject: [PATCH 5/6] Support user defined sizes in preview

Change-Id: I5d567755dc6a79be7c08cdd084954aaa874d6763
---
 vcl/source/window/printdlg.cxx | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/vcl/source/window/printdlg.cxx b/vcl/source/window/printdlg.cxx
index 8dec96bc585f..cc2cf09d44e9 100644
--- a/vcl/source/window/printdlg.cxx
+++ b/vcl/source/window/printdlg.cxx
@@ -1052,7 +1052,8 @@ void PrintDialog::preparePreview( bool i_bMayUseCache )
     {
         PrinterController::PageSize aPageSize =
             maPController->getFilteredPageFile( mnCurPage, aMtf, i_bMayUseCache );
-        aCurPageSize = aPrt->PixelToLogic(aPrt->GetPaperSizePixel(), MapMode(MapUnit::Map100thMM));
+        // Set user defined size for calc printing dialog
+        aCurPageSize = mbIsCalcPrintDialog ? aPageSize.aSize : aPrt->PixelToLogic(aPrt->GetPaperSizePixel(), MapMode(MapUnit::Map100thMM));
         if( ! aPageSize.bFullPaper )
         {
             const MapMode aMapMode( MapUnit::Map100thMM );
@@ -2251,6 +2252,24 @@ IMPL_LINK( PrintDialog, SelectHdl, weld::ComboBox&, rBox, void )
     {
         VclPtr<Printer> aPrt( maPController->getPrinter() );
         PaperInfo aInfo = aPrt->GetPaperInfo( rBox.get_active() );
+
+        // Restore user defined size when selected in list
+        if (aPrt->GetPaperInfoCount() <= rBox.get_active())
+        {
+            Size aOriginalPaperSize = getJobPageSize();
+            aInfo = PaperInfo(aOriginalPaperSize.getWidth(), aOriginalPaperSize.getHeight());
+        }
+        // Use as "Automatic" user defined orientation
+        else if (aPrt->GetPaperInfoCount() < rBox.get_count())
+        {
+            Size originalSize = getJobPageSize();
+            if ( (aInfo.getWidth() > aInfo.getHeight() && originalSize.Width() < originalSize.Height())
+              || (aInfo.getWidth() < aInfo.getHeight() && originalSize.Width() > originalSize.Height()) )
+            {
+                aInfo = PaperInfo(aInfo.getHeight(), aInfo.getWidth());
+            }
+        }
+
         aInfo.doSloppyFit(true);
         mePaper = aInfo.getPaper();
 
-- 
2.40.1

